package com.ww.java8.fp.lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

/**
 * Predicate函数式接口
 *
 * @author wanggw
 * @date 2022-11-07 23:41:47
 */
public class LambdaDemo2 {

    public static void main(String[] args) {
        List<String> listOfStrings = new ArrayList<>();
        listOfStrings.add("hello world");
        listOfStrings.add("");
        listOfStrings.add("world");
        listOfStrings.add("英文");
        Predicate<String> nonEmptyStringPredicate = (String s) -> !s.isEmpty();
        List<String> nonEmpty = filter(listOfStrings, nonEmptyStringPredicate);
        nonEmpty.stream().forEach(System.out::println);
    }

    public static <T> List<T> filter(List<T> list, Predicate<T> p) {
        List<T> results = new ArrayList<>();
        for (T t : list) {
            if (p.test(t)) {
                results.add(t);
            }
        }
        return results;
    }
}
